package com.huawei.mpchartexample;

import com.huawei.mpchartlib.charts.PieChart;
import com.huawei.mpchartlib.components.Legend;
import com.huawei.mpchartlib.data.PieData;
import com.huawei.mpchartlib.data.PieDataSet;
import com.huawei.mpchartlib.data.PieEntry;
import com.huawei.mpchartlib.formatter.PercentFormatter;
import com.huawei.mpchartlib.utils.ColorTemplate;
import com.huawei.mpchartlib.utils.MPPointF;
import ohos.aafwk.ability.Ability;
import ohos.aafwk.content.Intent;
import ohos.agp.colors.RgbColor;
import ohos.agp.components.Component;
import ohos.agp.components.Slider;
import ohos.agp.components.Text;
import ohos.agp.components.element.ShapeElement;
import ohos.agp.render.Canvas;
import ohos.agp.text.Font;
import ohos.agp.utils.Color;
import ohos.agp.window.service.WindowManager;

import java.util.ArrayList;

public class PieChartAbility extends Ability implements Slider.ValueChangedListener{

    private PieChart chart;
    private Slider sliderX, sliderY;
    private Text tvX, tvY;

    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        super.setUIContent(ResourceTable.Layout_ability_piechart);

        tvX = findComponentById(ResourceTable.Id_tvX);
        tvY = findComponentById(ResourceTable.Id_tvY);

        sliderX = findComponentById(ResourceTable.Id_sliderX);
        sliderX.setValueChangedListener(this);

        sliderY = findComponentById(ResourceTable.Id_sliderY);
        sliderY.setValueChangedListener(this);

        chart = (PieChart) findComponentById(ResourceTable.Id_pieChart);
        {   // // Chart Style // //

            // using percent value
            chart.setUsePercentValues(true);

            // background color
            ShapeElement background= new ShapeElement();
            background.setShape(ShapeElement.RECTANGLE);
            background.setRgbColor(new RgbColor(255,255,255));
            chart.setBackground(background);

            // disable description text
            chart.getDescription().setEnabled(false);

            // set offset
            chart.setExtraOffsets(5, 10, 5, 5);

            chart.setDragDecelerationFrictionCoef(0.95f);

            // add a selection listener
            chart.setOnChartValueSelectedListener(null);

            // entry label styling
            chart.setEntryLabelColor(Color.WHITE.getValue());
            chart.setEntryLabelTypeface(Font.DEFAULT_BOLD);
            chart.setEntryLabelTextSize(12f);
        }

        { // // Center Hole // //

            // center hole
            chart.setDrawHoleEnabled(true); // 饼状图中心的留空
            chart.setHoleColor(Color.WHITE.getValue()); // 留空的颜色
            chart.setHoleRadius(35f);   // 留空的半径

            // transparent circle
            chart.setTransparentCircleColor(Color.WHITE.getValue());
            chart.setTransparentCircleAlpha(110);
            chart.setTransparentCircleRadius(42f);

            chart.setCenterText("PieChart");
            chart.setDrawCenterText(true);
        }

        {   // // Rotation // //
            chart.setRotationAngle(0);
            chart.setRotationEnabled(false);
        }

        // add data
        sliderX.setProgressValue(5);
        sliderY.setProgressValue(10);
        setData(5, 10);
        chart.invalidate();

        {   // // Legend // //
            // get the legend (only possible after setting data)
            Legend l = chart.getLegend();

            l.setVerticalAlignment(Legend.LegendVerticalAlignment.TOP);
            l.setHorizontalAlignment(Legend.LegendHorizontalAlignment.LEFT);

            l.setOrientation(Legend.LegendOrientation.VERTICAL);
            l.setDrawInside(false);
            l.setXEntrySpace(7f);
            l.setYEntrySpace(0f);
            l.setYOffset(0f);
        }

        // draw chart
        Component.DrawTask drawTask = new Component.DrawTask() {
            @Override
            public void onDraw(Component component, Canvas canvas) {
                chart.onDraw(canvas);
            }
        };
        chart.addDrawTask(drawTask);
    }

    protected final String[] parties = new String[] {
            "Party A", "Party B", "Party C", "Party D", "Party E", "Party F", "Party G", "Party H",
            "Party I", "Party J", "Party K", "Party L", "Party M", "Party N", "Party O", "Party P",
            "Party Q", "Party R", "Party S", "Party T", "Party U", "Party V", "Party W", "Party X",
            "Party Y", "Party Z"
    };

    private void setData(int count, float range) {
        ArrayList<PieEntry> values = new ArrayList<>();
        values.add(new PieEntry(30, parties[0]));
        values.add(new PieEntry(20, parties[1]));
        values.add(new PieEntry(50, parties[2]));

        for (int i = 0; i < count ; i++) {
            float val = (float) ((Math.random() * range) + range / 5);
            values.add(new PieEntry(val, parties[i % parties.length]));
        }

        PieDataSet dataSet = new PieDataSet(values, "Election Results");

        dataSet.setDrawIcons(false);

        dataSet.setSliceSpace(3f);
        dataSet.setIconsOffset(new MPPointF(0, 40));
        dataSet.setSelectionShift(5f);

        {   // // set colors // //
            ArrayList<Integer> colors = new ArrayList<>();

            for (int c : ColorTemplate.VORDIPLOM_COLORS)
                colors.add(c);

            for (int c : ColorTemplate.JOYFUL_COLORS)
                colors.add(c);

            for (int c : ColorTemplate.COLORFUL_COLORS)
                colors.add(c);

            for (int c : ColorTemplate.LIBERTY_COLORS)
                colors.add(c);

            for (int c : ColorTemplate.PASTEL_COLORS)
                colors.add(c);

            colors.add(ColorTemplate.getHoloBlue());

            dataSet.setColors(colors);
        }

        PieData data = new PieData(dataSet);

        data.setValueFormatter(new PercentFormatter());
        data.setValueTextSize(11f);
        data.setValueTextColor(Color.WHITE.getValue());
        data.setValueFont(Font.DEFAULT);

        chart.setData(data);

        // undo all highlights
        chart.highlightValues(null);
    }

    @Override
    public void onTouchStart(Slider slider) {

    }

    @Override
    public void onTouchEnd(Slider slider) {
    }

    @Override
    public void onProgressUpdated(Slider slider, int i, boolean b) {
        tvX.setText(String.valueOf(sliderX.getProgress()));
        tvY.setText(String.valueOf(sliderY.getProgress()));
        setData(sliderX.getProgress(), sliderY.getProgress());
        // redraw
        chart.invalidate();
    }
}
